﻿using Microsoft.AspNetCore.SignalR;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace WebApplication_SignalRDemo
{
    public class ChatHub : Hub<IChatClient>
    {

        public async Task SendMessageToGroupAsAsync(string groupName, string message)
        {
            await Clients.Group(groupName).ReceiveMessageAsync(message);
        }

        public async Task SendMessageAsync(string message)
        {
            Console.WriteLine($"rec:{message}");
            await Clients.All.ReceiveMessageAsync(message);
        }

        public async Task AddToGroupAsync(string groupName)
        {
            await Groups.AddToGroupAsync(Context.ConnectionId, groupName);
        }

        public async Task RemoveGromGroupAsync(string groupName)
        {
            await Groups.RemoveFromGroupAsync(Context.ConnectionId, groupName);
        }

        public async Task SendPrivateMessageAsync(string userID, string message)
        {
            await Clients.User(userID).ReceiveMessageAsync(message);
        }

        public override Task OnConnectedAsync()
        {
            return base.OnConnectedAsync();
        }

        public override Task OnDisconnectedAsync(Exception exception)
        {
            return base.OnDisconnectedAsync(exception);
        }

        public async Task WinformAPPAsync(string name, string msg)
        {
            await Clients.All.ReciveUpdataeAsync($"Recive From {name}, Message content：{msg}");
        }
    }
}
